package com.jingtao.rabbitmq.consumer;

import com.jingtao.rabbitmq.constant.RabbitMQConstants;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * 工作队列模式消息消费者配置
 */
@Component
@Log4j2
public class WorkQueueConfig {

    /**
     * 消费者1监听工作队列消息
     * @param msg 接收到的消息内容
     * @throws InterruptedException 如果线程被中断则抛出InterruptedException
     */
    @RabbitListener(queuesToDeclare = { @Queue(name = RabbitMQConstants.WORK_QUEUE_NAME)})
    public void workQueue(String msg) throws InterruptedException {
        // 模拟处理消息的耗时操作
        Thread.sleep(200);
        log.info("消费者1接收到消息，队列名称：{}，消息的内容：{}", RabbitMQConstants.WORK_QUEUE_NAME, msg);
    }

    /**
     * 消费者2监听工作队列消息
     * @param msg 接收到的消息内容
     * @throws InterruptedException 如果线程被中断则抛出InterruptedException
     */
    @RabbitListener(queuesToDeclare = { @Queue(name = RabbitMQConstants.WORK_QUEUE_NAME)})
    public void workQueue2(String msg) throws InterruptedException {
        // 模拟处理消息的耗时操作
        Thread.sleep(400);
        log.info("消费者2接收到消息，队列名称：{}，消息的内容：{}", RabbitMQConstants.WORK_QUEUE_NAME, msg);
    }

}
